package com.hula.loan4u.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.hula.loan4u.activity.WebViewActivity;
import com.hula.loan4u.cons.AppCons;

import java.security.MessageDigest;


public class AppUtils {

    private static long backTime = 0;

    public static boolean exitApp() {
        if (System.currentTimeMillis() - backTime <= 2000) {
            return true;
        } else {
            backTime = System.currentTimeMillis();
            ToastAlone.show("再按一次退出程序");
            return false;
        }
    }

    public static String getAppVersion(Context context) {
        String appVersion = null;
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            appVersion = "V " + info.versionName;
        } catch (Exception e) {
            LogUtil.printeException(e);
        }
        return appVersion;
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }

        return false;
    }

    public static String getIMEI(Context context) {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
            return telephonyManager.getDeviceId();
        } catch (Exception e) {
            LogUtil.printeException(e);
            return null;
        }
    }

    public static String getAndroidId(Context context) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } catch (Exception e) {
            LogUtil.printeException(e);
            return null;
        }
    }

    public static String getDeviceId(Context context) {
        String imei = getIMEI(context);
        if (!TextUtils.isEmpty(imei)) {
            return md5Encrypt(imei);
        }
        String androidId = getAndroidId(context);
        if (!TextUtils.isEmpty(androidId)) {
            return md5Encrypt(androidId);
        }
        return "deviceidisnone";
    }

    public static boolean isWiFiConnected(Context context) {
        if (context != null) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager.getConnectionInfo() != null) {
                return true;
            }
        }

        return false;
    }


    public static String md5Encrypt(String string) {
        try {
            if (TextUtils.isEmpty(string)) {
                return null;
            }
            byte[] hash = null;
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                int i = (b & 0xFF);
                if (i < 0x10) hex.append('0');
                hex.append(Integer.toHexString(i));
            }
            return hex.toString();
        } catch (Exception e) {
            LogUtil.printeException(e);
        }
        return null;
    }

    public static String getQiNiuParam(int width, int height) {
        return "?imageView2/1/w/" + width + "/h/" + height;
    }

    public static void openBrowser(Context mContext, String url, String title) {
        try {
            Intent mIntent = new Intent();
            mIntent.setClass(mContext, WebViewActivity.class);
            mIntent.putExtra(AppCons.WEBVIEW_URL, url);
            mIntent.putExtra(AppCons.WEBVIEW_TITLE, title);
            mContext.startActivity(mIntent);
        } catch (Exception e) {
        }
    }

    public static String getTestHtml(int position) {
        return  "file:///android_asset/"+position+".html";
    }
}
